package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.nifa.NifaData;
import com.ucredit.babel.common.model.nifa.NifaQueryRecord;

/**
 * 互金数据
 * @author liubaoxin
 */
@Deprecated
public class NifaParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private String timestamstr="";
	private String job_id ="";
	private String system_id ="";
	private StringBuilder sb = new StringBuilder(500);
	private StringBuilder sb2 = new StringBuilder(500);
	private  Text val = new Text();
	 private Text key_text = new Text("nifa_query");
     private Text key_deail_text = new Text("nifa_query_detail");
	private JSONObject dataObj = null;
	 private JSONObject tmpObj = null;
	private String tmpstr=null;

	/**
	 * 互金数据
	 * @param userBehavior
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseNIFA(Context context,NifaQueryRecord nifaRecord,String successful) throws IOException, InterruptedException{
		 
    	Job job = nifaRecord.getJob();
    	NifaData nifaData = nifaRecord.getData();
    	 
    	sb.delete(0,sb.length());
    	//job_id
    	this.job_id = job.getJobID()==null?"":job.getJobID();
    	//system_id
    	this.system_id = job.getSystemID()==null?"":job.getSystemID().toString();
    	//数据时间 
    	this.timestamstr=nifaRecord.getTimestamp()==null?"":DateUtil.formateDateStr(new Date(nifaRecord.getTimestamp()));
    	 
 
    	sb.append(job_id).append(CRT001).append(system_id).append(CRT001).append(this.timestamstr).append(CRT001);
    	sb.append(nifaData.getName()==null?"":nifaData.getName()).append(CRT001);
    	sb.append(nifaData.getIdtype()==null?"":nifaData.getIdtype()).append(CRT001);
    	sb.append(nifaData.getNo()==null?"":nifaData.getNo()).append(CRT001);
    	tmpstr = sb.toString(); 
    	sb.append(nifaData.getReason()==null?"":nifaData.getReason()).append(CRT001);
    	String data_json = nifaData.getData();
    	 //解析data_json
        dataObj=JSONObject.parseObject(data_json);
        if(dataObj==null){
            sb.append("").append(CRT001).append("").append(CRT001);
            sb.append("").append(CRT001).append("").append(CRT001);
            sb.append("").append(CRT001).append("").append(CRT001);
            sb.append("").append(CRT001).append("").append(CRT001);
            sb.append("").append(CRT001).append("").append(CRT001);
            sb.append("").append(CRT001).append("");
        }else{
          sb.append(dataObj.getString("loancount")==null?"":dataObj.getString("loancount")).append(CRT001);
          sb.append(dataObj.getString("loanamt")==null?"":dataObj.getString("loanamt")).append(CRT001);
          sb.append(dataObj.getString("outstandcount")==null?"":dataObj.getString("outstandcount")).append(CRT001);
          sb.append(dataObj.getString("loanbal")==null?"":dataObj.getString("loanbal")).append(CRT001);
          sb.append(dataObj.getString("overduecount")==null?"":dataObj.getString("overduecount")).append(CRT001);
          sb.append(dataObj.getString("overdueamt")==null?"":dataObj.getString("overdueamt")).append(CRT001);
          sb.append(dataObj.getString("overduemorecount")==null?"":dataObj.getString("overduemorecount")).append(CRT001);
          sb.append(dataObj.getString("overduemoreamt")==null?"":dataObj.getString("overduemoreamt")).append(CRT001);
          sb.append(dataObj.getString("generationcount")==null?"":dataObj.getString("generationcount")).append(CRT001);
          sb.append(dataObj.getString("generationamount")==null?"":dataObj.getString("generationamount")).append(CRT001);
          sb.append(dataObj.getString("totalorg")==null?"":dataObj.getString("totalorg")).append(CRT001);
          sb.append(dataObj.getString("queryatotalorg")==null?"":dataObj.getString("queryatotalorg"));
          val.set(sb.toString());
          context.write(key_text, val);
        //解析查詢明細
          
          JSONArray jarr=dataObj.getJSONArray("infoquerybean");
          if(jarr!=null){
              for (int i = 0; i < jarr.size(); i++) {
                  sb2.delete(0, sb2.length());
                  tmpObj = jarr.getJSONObject(i);
                  sb2.append(tmpstr);
                  sb2.append(tmpObj.getString("ordernum")==null?"":tmpObj.getString("ordernum")).append(CRT001);
                  sb2.append(tmpObj.getString("ddate")==null?"":tmpObj.getString("ddate")).append(CRT001);
                  sb2.append(tmpObj.getString("s_value")==null?"":tmpObj.getString("s_value"));
                  val.set(sb2.toString());
                  
                  context.write(key_deail_text, val);
              }
             
          }
        }
    	 
	}
	
	
	
	
	
}
